/*
 * @lc app=leetcode.cn id=103 lang=cpp
 *
 * [103] 二叉树的锯齿形层序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void preorder(TreeNode* root,int Depth,vector<vector<int>> &res)
    {
        if(root == NULL)
            return ;

        if(Depth >= res.size())
            res.push_back(vector<int> {});

        if(Depth % 2 == 0)
        {
        //正序            
            res[Depth].push_back(root->val);
            preorder(root->left,Depth+1,res);
            preorder(root->right,Depth+1,res);
        }
        else
        {
            //逆序
            auto it = res[Depth].begin();
            res[Depth].insert(it,root->val);
            preorder(root->left,Depth+1,res);
            preorder(root->right,Depth+1,res);
        }

    }

    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> res;
        preorder(root,0,res);
        return res;
    }
};
// @lc code=end

